package storage.v1;

import java.util.ArrayList;
import java.util.LinkedHashSet;

import com.onpositive.data.IPropertyConstants;
import com.onpositive.knowledge.model.IThing;
import com.onpositive.mediawiki.propertyparser.ParsedDocument;
import com.onpositive.mediawiki.propertyparser.PropertyCharacteristic;
import com.onpositive.semantic.model.api.meta.BaseMeta;
import com.onpositive.semantic.model.api.meta.IMeta;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.units.ParsedFileList;
import com.onpositive.units.ParsedProperty;
import com.onpositive.units.ParsedString;
import com.onpositive.units.ParsedValue;
import com.onpositive.wikistorage.PictureManager;

public class ExternalDefaultPictureProperty implements IProperty {

	public static final String EXTERNAL_DEFAULT_PICTURE_PROPERTY_NAME = "external-default-picture";

	private IWikiStorage storage;
	private BaseMeta meta;

	public ExternalDefaultPictureProperty(IWikiStorage storage) {
		super();
		this.storage = storage;
		meta = new BaseMeta();
		meta.putMeta(PropertyCharacteristic.CHAR_PARSER_ID_LABEL,ParsedString.ID);
		meta.putMeta(PropertyCharacteristic.CHAR_PROPERTY_NAME_LABEL,EXTERNAL_DEFAULT_PICTURE_PROPERTY_NAME );
		meta.putMeta(IPropertyConstants.CHAR_IS_SERVICE_PROPERTY, true );
		meta.putMeta(IPropertyConstants.CHAR_DEPRICATED_LABEL, true );
	}

	@Override
	public String getId() {

		return EXTERNAL_DEFAULT_PICTURE_PROPERTY_NAME;
	}

	@Override
	public Object getValue(Object obj) {

		if (obj instanceof IThing) {
			obj = ((IThing) obj).getId();
		}
		if (obj instanceof String) {
			String id = (String) obj;
			ParsedDocument pd = storage.getParsedDocument(id);
			if(pd == null)
				return null;
			PictureManager instance = null;
			try{
				instance = PictureManager.getInstance(pd.getRootParh());
			}catch(Exception e)
			{
				e.printStackTrace() ;				
			}
			
			if (pd.getDefaultPicture() != null&&pd.getDefaultPicture().length()>0) {
				if (pd != null) {
				
					return instance
							.convertToLocalPath(pd.getDefaultPicture(),
									pd.getName());
				}
			}
			ParsedProperty property = pd.getProperty(IPropertyConstants.PICTURE_LIST_PROPERTY_NAME);
			if (property!=null){
				LinkedHashSet<ParsedValue> values = property.getValues();
				if (values!=null){
					for (ParsedValue vl:values){
						if (vl instanceof ParsedFileList){
							ParsedFileList fl=(ParsedFileList) vl;
							ArrayList<String> valuesList = fl.getValuesList();							
							for (String str:valuesList){
								boolean checkPicturePresence = instance.checkPicturePresence(str, id);
//								if (!checkPicturePresence){
//									instance.grabPictureFromNet(str, id);
//								}
//								checkPicturePresence = instance.checkPicturePresence(str, id);
								if (checkPicturePresence){
									String localPath = instance.convertToLocalPath(str,	pd.getName());
									return localPath;
								}
							}
						}
					}
				}
			}
			//instance.saveValidPicsList() ;
		}
		return null;
	}

	@Override
	public IMeta getMeta() {

		return meta;
	}

}
